<?php

declare(strict_types=1);

namespace App\Controller\Call;


use App\Constants\CodeData;
use App\Controller\BaseController;
use App\Exception\MessageException;
use App\Request\Call\CallAnserRequest;
use App\Request\Call\CallCoverRequest;
use App\Request\Call\CallFreeRequest;
use App\Request\Call\CallHangUpRequest;
use App\Request\Call\CallRefuseRequest;
use App\Services\Call\CallService;
use App\Utils\FunUtil;
use Hyperf\Context\Context;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Contract\RequestInterface;

class CallController extends BaseController
{
    #[Inject]
    public CallService $callService;

    /**
     * Note - 对另一方发起视频语音呼叫
     * DateTime 2024/7/26 11:38
     * User Admin
     * @param RequestInterface $request
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function call()
    {
        $userInfo = Context::get('user');
        $params = $this->request->post();
        // 禁言判断
        if (FunUtil::gagUserChat($userInfo['id'])) {
            throw new MessageException("账号异常正在风险审核中，审核通过才可以使用该功能，请耐心等候。", CodeData::NEED_TIPS);
        }
        $result = $this->callService->call($params, $userInfo);
        return $this->return($result);
    }


    /**
     * Note - 免费匹配的用户
     * DateTime 2024/7/26 11:38
     * User Admin
     * @param RequestInterface $request
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function getMatchingUsers(CallFreeRequest $request)
    {
        $params = $this->request->post();
        $request->execute($params);
        $userInfo = Context::get('user');
        $result = $this->callService->getMatchingUsers($userInfo, $params);
        return $this->return($result);
    }

    /**
     * Note - 获取通话列表
     * DateTime 2024/7/26 11:38
     * User Admin
     * @param RequestInterface $request
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function getCallList()
    {
        $userInfo = Context::get('user');
        $params = $this->request->post();
        $params['language'] = Context::get('language');
        $result = $this->callService->getCallList($userInfo, $params);
        return $this->return($result);
    }


    /**
     * Note - 获取、设置通话封面
     * DateTime 2024/8/20 18:27
     * User Admin
     * @param CallCoverRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function callCover(CallCoverRequest $request)
    {
        $params = $this->request->post();
        $request->execute($params);
        $userInfo = Context::get('user');
        if ($userInfo['sex'] == 1) {
            // Tool::JSON(\CodeData::NEED_TIPS, ['result' => 0], "仅限女用户设置！");
            throw new MessageException("用户不支持该设置");
        }
        $result = $this->callService->callCover($userInfo['id'], $params);
        return $this->return($result);

    }


    /**
     * Note - 接听方拒绝接听，通知服务器通知发起方
     * DateTime 2024/7/26 11:38
     * User Admin
     * @param CallRefuseRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function refuse(CallRefuseRequest $request)
    {
        $params = $this->request->post();
        $request->execute($params);
        $userInfo = Context::get('user');
        $result = $this->callService->refuse($userInfo, $params);
        return $this->return($result);
    }


    /**
     * Note - 接听方接听，通知服务器
     * DateTime 2024/8/21 14:06
     * User Admin
     * @param CallAnserRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function answer(CallAnserRequest $request)
    {

        $params = $this->request->post();
        $request->execute($params);
        $userInfo = Context::get('user');
        $result = $this->callService->answer($userInfo, $params);
        return $this->return($result);
    }


    /**
     * Note - 挂断
     * DateTime 2024/8/21 15:41
     * User Admin
     * @param CallHangUpRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function hangUp(CallHangUpRequest $request)
    {
        $params = $this->request->post();
        $request->execute($params);
        $userInfo = Context::get('user');
        $result = $this->callService->hangUp($userInfo, $params);
        return $this->return($result);

    }

}
